package ljt.music.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jiangtao
 */
public class BasePagerAdapter extends FragmentPagerAdapter {

  private List<Fragment> fragmentList;
  private List<String> titleList;

  public BasePagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
    super(fm);
    this.fragmentList = fragmentList;
  }

  public BasePagerAdapter(FragmentManager fm, List<Fragment> fragmentList, String[] titles) {
    super(fm);
    this.fragmentList = fragmentList;
    this.titleList = new ArrayList<>(Arrays.asList(titles));
  }

  public BasePagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) {
    super(fm);
    this.fragmentList = fragmentList;
    this.titleList = titleList;
  }

  @Override
  public Fragment getItem(int position) {
    return fragmentList.get(position);
  }

  @Override
  public int getCount() {
    if (fragmentList != null) {
      return fragmentList.size();
    }
    return 0;
  }

  @Override
  public CharSequence getPageTitle(int position) {
    if (titleList != null) {
      return titleList.get(position);
    }
    return "";
  }

  @Override
  public int getItemPosition(Object object) {
    return PagerAdapter.POSITION_NONE;
  }

  public void recreateItems(List<Fragment> fragmentList, List<String> titleList) {
    this.fragmentList = fragmentList;
    this.titleList = titleList;
    notifyDataSetChanged();
  }
}
